package org.grayrabbit.cms.plugin.servlet;

/**
 * @author sys53
 * @version 1.0
 * @created 06-一月-2012 17:14:56
 */
public class ResourceUrlParser {

	private final String strategyPrefix;

	public ResourceUrlParser(String strategyPrefix) {
		this.strategyPrefix = strategyPrefix;
	}

	/**
	 * 
	 * @param resourceUrl
	 */
	public PluginResource parse(String resourceUrl) {
		if (!matches(resourceUrl)) {
			return null;
		}
		int indexOfStrategyPrefix = resourceUrl.indexOf(this.strategyPrefix);
		String libraryAndResource = resourceUrl.substring(indexOfStrategyPrefix+ this.strategyPrefix.length() + 1);
		String[] parts = libraryAndResource.split("/", 2);

		if (parts.length != 2)
			return null;
		return new PluginResource(parts[0], parts[1]);
	}

	public boolean matches(String resourceUrl) {
		return resourceUrl.indexOf(BaseFileServerServlet.SERVLET_PATH + "/" + this.strategyPrefix) != -1;
	}

}